import {Platform} from 'react-native';

function isIos() {
	return !isAndroid();
}

function isAndroid() {
	return Platform['OS'] == 'android';
}

/**
 * 对象克隆
 * @param {Object} obj
 */
function clone(obj) {
	let _className = function(o) {
		if(o === null) return "Null";
		if(o === undefined) return "Undefined";
		return Object.prototype.toString.call(o).slice(8, -1);
	}
	let result, oClass = _className(obj);
	//确定result的类型
	if(oClass === "Object") {
		result = {};
	} else if(oClass === "Array") {
		result = [];
	} else {
		return obj;
	}
	for(let key in obj) {
		let copy = obj[key];
		if(_className(copy) == "Object" || _className(copy) == "Array") {
			result[key] = clone(copy); //递归调用
		} else {
			result[key] = obj[key];
		}
	}
	return result;
}

/**
 * yyyy-MM-dd HH:mm:ss:SS格式转Date
 * @param {Object} dateStr
 */
function toDate(dateStr) {
	// 从格式化子串获取日期
	var date = new Date();
	var cut = function(start, end) {
		var res = null;
		return parseInt(dateStr.substring(start, end), 10);
	};
	if(cut(0, 4)) {
		date.setYear(cut(0, 4));
	} else {
		date.setYear(0);
	}
	if(cut(5, 7)) {
		date.setMonth(cut(5, 7) - 1);
	} else {
		date.setMonth(0);
	}
	if(cut(8, 10)) {
		date.setDate(cut(8, 10));
	} else {
		date.setDate(0);
	}
	if(cut(11, 13)) {
		date.setHours(cut(11, 13));
	} else {
		date.setHours(0);
	}
	if(cut(14, 16)) {
		date.setMinutes(cut(14, 16));
	} else {
		date.setMinutes(0);
	}
	if(cut(17, 19)) {
		date.setSeconds(cut(17, 19));
	} else {
		date.setSeconds(0);
	}
	if(cut(20, 22)) {
		date.setMilliseconds(cut(20, 22));
	} else {
		date.setMilliseconds(0);
	}
	return date;
}

export {
	isIos,
	isAndroid,
	clone,
	toDate,
}